#!/usr/bin/env python
#coding=utf-8

import json
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkcore.auth.credentials import AccessKeyCredential
from aliyunsdkcore.auth.credentials import StsTokenCredential
from aliyunsdkalidns.request.v20150109.DescribeDomainRecordsRequest import DescribeDomainRecordsRequest

def get_dns(key,secret,domain):
    # Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
    credentials = AccessKeyCredential(key, secret)
    # use STS Token
    # credentials = StsTokenCredential(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'], os.environ['ALIBABA_CLOUD_SECURITY_TOKEN'])
    client = AcsClient(region_id='cn-shenzhen', credential=credentials)

    request = DescribeDomainRecordsRequest()
    request.set_accept_format('json')

    request.set_DomainName(domain)
    request.set_PageNumber(1)
    request.set_PageSize(500)
    request.set_Status("Enable")

    response = client.do_action_with_exception(request)
    # python2:  print(response) 
    return json.loads(response)

# #获取域名解析列表
# if __name__ == '__main__':
#     for i in get_all_dns('LTAI5tNxx5b9poTZSKNXxNAB','VDlkjMHSbFANRwxrZtMB1OhC3DetNG','e12j.com')['DomainRecords']['Record']:
#         print(i['RR'])